package xyz.yuanjilu.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import xyz.yuanjilu.annotation.SysLog;
import xyz.yuanjilu.entity.GroupInfoEntity;
import xyz.yuanjilu.service.GroupInfoService;
import xyz.yuanjilu.utils.PageUtils;
import xyz.yuanjilu.utils.Query;
import xyz.yuanjilu.utils.R;


/**
 * 群组管理
 * 
 * @author CHENJUNFEI
 * @email kkomge@qq.com
 * @date 2017-07-19 14:54:21
 */
@RestController
@RequestMapping("groupinfo")
public class GroupInfoController extends WebMagicAbstractController{
	@Autowired
	private GroupInfoService groupInfoService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("groupinfo:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<GroupInfoEntity> groupInfoList = groupInfoService.queryList(query);
		int total = groupInfoService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(groupInfoList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("groupinfo:info")
	public R info(@PathVariable("id") Long id){
		GroupInfoEntity groupInfo = groupInfoService.queryObject(id);
		
		return R.ok().put("groupInfo", groupInfo);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存分组")
	@RequestMapping("/save")
	@RequiresPermissions("groupinfo:save")
	public R save(@RequestBody GroupInfoEntity groupInfo){
		groupInfoService.save(groupInfo);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改分组")
	@RequestMapping("/update")
	@RequiresPermissions("groupinfo:update")
	public R update(@RequestBody GroupInfoEntity groupInfo){
		groupInfoService.update(groupInfo);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除分组")
	@RequestMapping("/delete")
	@RequiresPermissions("groupinfo:delete")
	public R delete(@RequestBody Long[] ids){
		groupInfoService.deleteBatch(ids);
		
		return R.ok();
	}
	
	/**
     * 绑定站点,
     */
	@SysLog("绑定站点")
    @RequestMapping("/bindwebsite")
    public R bindwebsite(@RequestBody BindwebsiteForm2 bindwebsiteForm){
        System.err.println(bindwebsiteForm);
        groupInfoService.bindWebsite(bindwebsiteForm.getId(), bindwebsiteForm.getSelectWebIds());
        return R.ok();
    }
    /**
     * 绑定销售,
     */
	@SysLog("绑定销售")
    @RequestMapping("/bindsales")
    public R bindsales(@RequestBody BindwebsiteForm2 bindwebsiteForm){
        System.err.println(bindwebsiteForm);
        groupInfoService.bindSales(bindwebsiteForm.getId(), bindwebsiteForm.getSalesIds());
        return R.ok();
    }
	
}

class BindwebsiteForm2{
    String id;
    String[] selectWebIds;
    String[] salesIds;
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String[] getSelectWebIds() {
        return selectWebIds;
    }
    public void setSelectWebIds(String[] selectWebIds) {
        this.selectWebIds = selectWebIds;
    }
    
    public String[] getSalesIds() {
        return salesIds;
    }
    public void setSalesIds(String[] salesIds) {
        this.salesIds = salesIds;
    }
    
    
    
}